//
//  NSObject+HKSwizzling.m
//  HKUtilities
//
//  Created by 胡锦涛 on 2018/11/3.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "NSObject+HKSwizzling.h"
#import <objc/runtime.h>
@implementation NSObject (HKSwizzling)

- (void)willDealloc {
    //监听是否存活
    __weak id weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        __strong id strongSelf = weakSelf;
       NSLog(@"leak_WillDealloc---%@",strongSelf);//若此方法执行，则有内存泄漏，该对象未被释放
    });
}
+ (void)swizzleSEL:(SEL)originalSEL withSEL:(SEL)swizzlingSEL {
    Class class = [self class];
    Method originMethod = class_getInstanceMethod(class, originalSEL);
    Method currentMethod = class_getInstanceMethod(class, swizzlingSEL);
    
    BOOL didAddMethod = class_addMethod(class, originalSEL, method_getImplementation(currentMethod), method_getTypeEncoding(currentMethod));
    if (didAddMethod) {
        class_replaceMethod(class, swizzlingSEL, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else {
        method_exchangeImplementations(originMethod, currentMethod);
    }
}
@end
